package com.martin.iconparse;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhangyang
 */
public class MainActivity extends Activity {

    EditText mEdtUrl;

    ImageView mImgView;

    TextView mTxtProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mImgView = findViewById(R.id.img_icon);

        mEdtUrl = findViewById(R.id.edt_url);

        mTxtProcess = findViewById(R.id.txt_process);

        EventBus.getDefault().register(this);
    }

    /**
     * 解析点击
     *
     * @param view
     */
    public void onClickParse(View view) {

        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                String iconUrl = Utils.getIconByUrl(mEdtUrl.getText().toString());
                if (TextUtils.isEmpty(iconUrl)) {
                    emitter.onError(new Throwable("icon is null or parse error !"));
                    return;
                }
                emitter.onNext(iconUrl);
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //正常情况下需要，通过 RxLifecycle 或者 AutoDispose 进行生命周期管理，目前只有一个 ImageView 的图片设置，Glide 在 Activity 销毁的时候，会自动取消图片加载
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String url) {
                        GlideApp.with(MainActivity.this)
                                .load(url)
                                .placeholder(R.drawable.ic_launcher_background)
                                .fitCenter()
                                .into(mImgView);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MainActivity.this, "网站没有有效的图标", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 更新解析进度信息
     *
     * @param parseProcess
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventParseProcess parseProcess) {
        mTxtProcess.setText(parseProcess.getShowInfo());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
